<template>
  <div class="order-wrapper">
    <div class="search-container">
      <div class="order-num-container search-item">
        <div class="title">订单号：</div>
        <el-input v-model="order_no" type="text" placeholder="请输入订单号" class="input-container" />
      </div>
      <div class="order-num-container search-item">
        <div class="title">用户：</div>
        <el-input v-model="username" type="text" placeholder="请输入用户" class="input-container" />
      </div>
      <div class="order-num-container search-item">
        <div class="title">时间：</div>
        <el-date-picker
          v-model="timer"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
      <div class="order-num-container search-item">
        <div class="title">支付方式：</div>
        <el-select v-model="payment" placeholder="请选择"><el-option v-for="item in payOptions" :key="item.label" :label="item.value" :value="item.value" /></el-select>
      </div>
      <el-button type="primary" class="search-button" @click="handleSearch">搜索</el-button>
    </div>
    <el-table ref="multipleTable" v-loading="loading" :data="tableData" border tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="order_no" label="订单编号" min-width="200" />
      <el-table-column prop="username" label="用户" min-width="150" />
      <el-table-column prop="amount" label="金额" min-width="150" />
      <el-table-column prop="payment" label="支付方式" width="300">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payment === 'wepay'" type="success">微信</el-tag>
          <el-tag v-if="scope.row.payment === 'alipay'" type="success">支付宝</el-tag>
          <el-tag v-if="scope.row.payment === 'balance'" type="success">余额</el-tag>
          <el-tag v-if="scope.row.payment === 'deposit'" type="success">储值</el-tag>
          <el-tag v-if="scope.row.payment === 'consume'" type="success">消费额</el-tag>
          <el-tag v-if="scope.row.payment === 'combined'" type="success">组合支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="备注" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column prop="pay_at" label="支付时间" min-width="100" />
      <el-table-column prop="payment" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="success" @click="handleClick(scope)">打印</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>listQuery.limit" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      title="订单小票打印信息"
      :visible.sync="packagePrint"
      :append-to-body="true"
      style="padding: 0"
      class="print-dialog"
      width="300px"
      :center="true"
    >
      <section v-if="showInfo" id="offline-html">
        <div class="print-img"><img src="../../../assets/avatar.png"></div>
        <div class="title">欢迎使用驴优客</div>
        <div class="star"><span v-for="(item, index) of 26" :key="index">*</span></div>
        <div class="desc">门店： {{ info.shop_name.toString().substr(0, 14) }}</div>
        <div class="desc">用户： {{ info.username.toString().substr(0,3) }}****{{ info.username.toString().substr(-4) }}</div>
        <div class="desc">订单号：{{ info.order_no.toString().substr(0, 14) }}</div>
        <div class="desc">交易时间：{{ parseTimeFuc(info.create_at) }}</div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <div class="prop">
          <p>合计金额：</p>
          <p>{{ info.amount }}</p>
        </div>
        <div class="prop">
          <p>支付状态：</p>
          <p>已支付</p>
        </div>
        <div class="star"><span v-for="(item,index) of 26" :key="index">*</span></div>
        <p class="desc1">此小票仅供对账存留</p>
        <p class="desc1">感谢您的使用，谢谢！</p>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="packagePrint = false">取 消</el-button>
        <el-button type="primary" @click="handlePrint">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import printJS from 'print-js'
import { offlineOrder } from '@/api/order'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
export default {
  name: 'OrderList',
  components: {
    Pagination
  },
  props: {
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      order_no: '',
      username: '',
      total: 0,
      phone: '',
      listQuery: {
        page: 1,
        limit: 10
      },
      list: [],
      timer: [],
      payment: '全部',
      parseTimeFuc: parseTime,
      payOptions: [
        {
          label: '',
          value: '全部'
        },
        {
          label: 'wepay',
          value: '微信'
        },
        {
          label: 'alipay',
          value: '支付宝'
        },
        {
          label: 'balance',
          value: '余额'
        },
        {
          label: 'deposit',
          value: '储值'
        },
        {
          label: 'consume',
          value: '消费额'
        },
        {
          label: 'combined',
          value: '组合支付'
        }
      ],
      packagePrint: false,
      info: {}
    }
  },
  computed: {
    tableData() {
      return this.list.map((item) => {
        item.pay_at = item.pay_at ? parseTime(item.pay_at) : ''
        item.desc = item.desc ? item.desc : ''
        return item
      })
    },
    showInfo() {
      return Object.keys(this.info).length
    }
  },
  created() {
    this.initList()
  },
  methods: {
    // 分页
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.initList()
    },
    async initList() {
      this.loading = true
      const { order_no, username, payment, payOptions, timer } = this
      const start_time = timer.length ? Math.round(+new Date(timer[0]) / 1000) : ''
      const end_time = timer.length ? Math.round(+new Date(timer[1]) / 1000) : ''
      const { page, limit } = this.listQuery
      const newPayment = this.transformantClassify(payOptions, payment).label
      const res = await offlineOrder({ order_no, username, page, limit, payment: newPayment, start_time, end_time })
      this.loading = false
      const { total, lists } = res.data
      this.total = total
      this.list = lists
    },
    handleSearch() {
      this.initList()
    },
    handledetail(scope) {
      const { id } = scope.row
      this.$router.push({
        path: `/order/roomTime/roomTimeDetails`,
        query: {
          id
        }
      })
    },
    transformantClassify(array, type) {
      return array.find((item, index) => {
        if (item.value === type) {
          return item
        }
      })
    },
    handleClick(scope) {
      this.packagePrint = true
      this.info = scope.row
    },
    handlePrint() {
      printJS({
        printable: 'offline-html',
        type: 'html',
        style: '#offline-html{width: 100%}.title{ font-size: 14px; text-align: center; }.print-img{width: 100%;text-align: center; } img{;width: 100px; height: 100px}.desc{font-size: 12px; width:100%;transform：scale(0.8)}.prop{font-size: 12px;display:flex;justify-content: space-between;padding:0 4px;box-sizing:border-box; line-height: 16px}.desc1{text-align: center; font-size: 10px}',
        scanStyles: false
        // targetStyles: ['*']
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.order-wrapper {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  .search-container{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 20px;
    .search-item {
      display: flex;
      align-items: center;
      margin-right: 20px;
      margin-bottom: 10px;
      .title {
        width: 80px;
      }
      .input-container{
        width: 200px;
      }
    }
  }
}
#offline-html {
  padding: 20px;
  box-sizing: border-box;
  width: 190px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin: 0;
  font-size: 10px;
  img{
    width: 100px;
    height: 100px;
  }
  .desc{
    // width: 300px;
    width: 100%;
    line-height: 20px;
  }
  .prop{
    display: flex;
    width: 100%;
    justify-content: space-between;
  }
  .print-tr{
    padding:4px;
  }
  table{
    position: relative;
    margin-bottom: 10px;
  }
  table::before {
    width: 100%;
    height: 1px;
    content: "";
    position: absolute;
    background-color: #ebeef5;
    z-index: 1;
    bottom: -4px;
  }
  td{
    text-align: center;
    max-width: 100px;
    width: 100px;
  }
  .star{
    margin: 5px 0;
  }
}
</style>
